#include <cstdio>
#include <cstdlib>

#define TestModel(x) if (x) {
#define END   }

enum TestModels
{
    NuneStruct = false,
    NuneArray  = false,
    UnionUse   = true,
};

struct SoftArray
{
    int len;
    int array[];
};

struct TS
{

};
 
int system_mode()
{
    union SM
    {
        int i;
        char c;
    };

    union SM sm;
    
    sm.i = 1;
    
    return sm.c;
}

SoftArray* create_soft_array(int size)
{
    SoftArray* ret = NULL;
    
    if( size > 0 )
    {
        // 柔性数组，用空数组，建立动态数组
        ret = (SoftArray*)malloc(sizeof(SoftArray) + sizeof(int) * size);
        ret->len = size;
    }
    
    return ret;
}

void delete_soft_array(struct SoftArray* sa)
{
    free(sa);
}

void func(struct SoftArray* sa)
{
    int i = 0;
    
    if( NULL != sa )
    {
        for(i=0; i<sa->len; i++)
        {
            sa->array[i] = i + 1;
        }
    } 
}

void FunTest()
{
TestModel(NuneStruct)
    struct TS t1;
    struct TS t2;
    
    printf("sizeof(struct TS) = %d\n", sizeof(struct TS));
    printf("sizeof(t1) = %d, &t1 = %p\n", sizeof(t1), &t1);
    printf("sizeof(t2) = %d, &t2 = %p\n", sizeof(t2), &t2);
END

TestModel(NuneArray)
    int i = 0;
    SoftArray* sa = create_soft_array(10);
    
    func(sa);
    
    for(i = 0; i < sa->len; i++)
    {
        printf("%d\n", sa->array[i]);
    }
    
    delete_soft_array(sa);
END

TestModel(UnionUse)
    printf("System Mode: %d\n", system_mode());
END
}




int main()
{
    FunTest();
    return 0;
}




